-
Notifications
You must be signed in to change notification settings - Fork 19
add support for two-digit year in yeear selection (63095) #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
svenwey
wants to merge
2
commits into
master
Choose a base branch
from
feature/63095-correctly_interpret_twodigit_year
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Kagemaru
requested changes
Jun 2, 2025
Comment on lines
130
to
160
def parse_date(date) | ||
return nil if date.blank? | ||
|
||
if date.is_a? String | ||
if date.is_a?(String) | ||
if date.match?(/\A\d{1,2}\.\d{1,2}\.\d{2}\z/) | ||
day, month, short_year = date.split('.').map(&:to_i) | ||
|
||
# Get current full year and its century | ||
current_year = Time.zone.today.year | ||
cutoff = (current_year + 5) % 100 | ||
current_century = (current_year / 100) * 100 | ||
|
||
full_year = if short_year <= cutoff | ||
current_century + short_year | ||
else | ||
(current_century - 100) + short_year | ||
end | ||
|
||
return Date.new(full_year, month, day) | ||
end | ||
|
||
begin | ||
date = Date.strptime(date, I18n.t('date.formats.default')) | ||
rescue StandardError | ||
rescue ArgumentError | ||
date = Date.parse(date) | ||
end | ||
end | ||
date = date.to_date if date.is_a? Time | ||
|
||
date = date.to_date if date.is_a?(Time) | ||
date | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strptime
can handle the parsing of two year dates. Locale specific formats are handled in Rails with the I18n gem.
I'd rewrite this method as:
def parse_date(date)
return nil if date.blank?
case date
when Time then date.to_date
when /\A\d{1,2}\.\d{1,2}\.\d{2}\z/ then parse_date_string(date, :short_year)
when String then parse_date_string(date)
else date
end
end
private
def parse_date_string(date, type=:default)
Date.strptime(date, I18n.t(type, scope: 'date.formats'))
rescue ArgumentError
Date.parse(date)
end
...
and add the following to config/locales/crud.de-CH.yml
:
de-CH:
...
# formats
date:
formats:
short_year: "%d.%m.%y"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a bit hacky and not really clean in the general case (e.g. if used with a potential US format). However, as the 2-digit format itself is not really clean, I can't think of a better approach.